package com.baomidou.springwind.controller.data;

import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.Project;
import com.baomidou.springwind.entity.Resource;
import com.baomidou.springwind.enums.ResourceEnum;
import com.baomidou.springwind.service.ProjectService;
import com.baomidou.springwind.service.ResourceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 入库数据控制器
 * </p>
 *
 * @Author demo
 * @Date 2017/06/18
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private ProjectService projectService;

    @Permission("1001")
    @RequestMapping("/list")
    public String list(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/res_list", model);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/getData")
    public String getData(Page<Resource> page, String batchId) {
        page = new Page<Resource>(page.getCurrent(), page.getSize());
        EntityWrapper<Resource> wrapper = new EntityWrapper<Resource>();
        if (!StringUtils.isBlank(batchId)) {
            wrapper.eq("batch_Id", batchId);
        }
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        wrapper.orderBy("res_type");
        page = resourceService.selectPage(page, wrapper);
        List<Resource> resourceList = page.getRecords();
        if (resourceList.size() > 0) {
            Project project = projectService.selectById(resourceList.get(0).getBatchId());
            for (Resource res : resourceList) {
                res.setBatchName(project == null ? "--" : project.getName());
                res.setResName(ResourceEnum.valueOf(res.getResType()).getDesc());
            }
        }
        return jsonPage(page);
    }
}
